package cim3d;

import _61970.core.SubGeographicalRegion;
import _61970.core.Substation;
import _61970.core.Terminal;
import _61970.core.IdentifiedObject;
import _61970.meas.Analog;
import _61970.meas.AnalogValue;
import _61970.meas.MeasurementType;
import _61970.wires.ACLineSegment;
import com.ht.aems.ntp.CIMConstants;
import com.ht.aems.ntp.SystemModel;
import com.mxgraph.model.mxCell;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-5-26
 */
public class CIMAttriBuilder implements CIMConstants, CIMViewConst {
    public static String create(String resourceId, String attriName) {
        return resourceId + "_" + attriName;
    }

    private static void setDeviceName(IdentifiedObject resource, mxCell cell) {
        if(resource.getAliasName() != null)
            cell.setAttribute(ATTRI_DEV_NAME, resource.getAliasName());
        else if(resource.getName() != null)
            cell.setAttribute(ATTRI_DEV_NAME, resource.getName());
        else
            cell.setAttribute(ATTRI_DEV_NAME, resource.getMRID());
    }

    public static void formAttribute(String id, mxCell cell) {
        Object resource = SystemModel.INSTANCE.getResourceManager().getResource(id);
        if (resource == null) return;
        if (resource instanceof ACLineSegment) {
            ACLineSegment line = (ACLineSegment) resource;
            cell.setAttribute(ATTRI_DEV_TYPE, DEV_TYPE_ACLINE);
            setDeviceName(line, cell);
            cell.setAttribute(ATTRI_LINE_SWITCH, create(line.getMRID(), ATTRI_LINE_SWITCH));
            formLineMeasure((Terminal) line.getTerminals().get(0), cell);
            formLineMeasure((Terminal) line.getTerminals().get(1), cell);
        } else if (resource instanceof Substation) {
            Substation s = (Substation) resource;
            setDeviceName(s, cell);
            if (SystemModel.INSTANCE.getPowerPlants().containsKey(s.getMRID())) {
                cell.setAttribute(ATTRI_DEV_TYPE, DEV_TYPE_PLANT);
                cell.setAttribute(ATTRI_GEN_MAX_MW, create(s.getMRID(), ATTRI_GEN_MAX_MW));
                cell.setAttribute(ATTRI_GEN_MW, create(s.getMRID(), ATTRI_GEN_MW));
                cell.setAttribute(ATTRI_GEN_MAX_MVAR, create(s.getMRID(), ATTRI_GEN_MAX_MVAR));
                cell.setAttribute(ATTRI_GEN_MVAR, create(s.getMRID(), ATTRI_GEN_MVAR));
            } else {
                cell.setAttribute(ATTRI_DEV_TYPE, DEV_TYPE_STATION);
                cell.setAttribute(ATTRI_MAX_MVAR, create(s.getMRID(), ATTRI_MAX_MVAR));
                cell.setAttribute(ATTRI_MVAR, create(s.getMRID(), ATTRI_MVAR));
            }
        } else if (resource instanceof SubGeographicalRegion) {
            SubGeographicalRegion region = (SubGeographicalRegion) resource;
            cell.setAttribute(ATTRI_DEV_TYPE, DEV_TYPE_REGION);
            setDeviceName(region, cell);
            cell.setAttribute(ATTRI_REGION_MAX_MW, create(region.getMRID(), ATTRI_REGION_MAX_MW));
            cell.setAttribute(ATTRI_REGION_MW, create(region.getMRID(), ATTRI_REGION_MW));
            cell.setAttribute(ATTRI_REGION_MAX_MVAR, create(region.getMRID(), ATTRI_REGION_MAX_MVAR));
            cell.setAttribute(ATTRI_REGION_MVAR, create(region.getMRID(), ATTRI_REGION_MVAR));
        }
    }

    private static void formLineMeasure(Terminal terminal, mxCell cell) {
        java.util.List measList = terminal.getMeasurements();
        String id = cell.getAttribute(ATTRI_LINE_START_TERMINAL_ID);
        if (id == null) return;
        for (Object meas : measList) {
            if (meas instanceof Analog) {
                Analog measure = (Analog) meas;
                java.util.List measValueList = measure.getContain_MeasurementValues();
                if (measValueList == null || measValueList.size() == 0)
                    continue;
                MeasurementType type = measure.getMeasurementType();
                if (type == null || type.getName() == null)
                    continue;
                if (type.getName().equals(TYPE_THREE_PHASE_ACTIVE_POWER))
                    for (Object measValue : measValueList) {
                        AnalogValue analogValue = ((AnalogValue) measValue);
                        if (analogValue.getMeasurementValueSource().getName().equals(MEASURE_VALUE_SOURCE_SCADA)) {
                            if (id.equals(terminal.getMRID()))
                                cell.setAttribute(ATTRI_LINE_P_HEAD, analogValue.getMRID().substring(analogValue.getMRID().indexOf("_") + 1));
                            else
                                cell.setAttribute(ATTRI_LINE_P_TAIL, analogValue.getMRID().substring(analogValue.getMRID().indexOf("_") + 1));
                            break;
                        }
                    }
                else if (type.getName().equals(TYPE_THREE_PHASE_REACTIVE_POWER))
                    for (Object measValue : measValueList) {
                        AnalogValue analogValue = ((AnalogValue) measValue);
                        if (analogValue.getMeasurementValueSource().getName().equals(MEASURE_VALUE_SOURCE_SCADA)) {
                            if (id.equals(terminal.getMRID()))
                                cell.setAttribute(ATTRI_LINE_Q_HEAD, analogValue.getMRID().substring(analogValue.getMRID().indexOf("_") + 1));
                            else
                                cell.setAttribute(ATTRI_LINE_Q_TAIL, analogValue.getMRID().substring(analogValue.getMRID().indexOf("_") + 1));
                            break;
                        }
                    }
            }
        }
    }
}
